<template>
	<view class="jnpf-wrap jnpf-wrap-workflow">
		<u-form :model="dataForm" ref="dataForm" :errorType="['toast']" label-position="left" label-width="150"
			label-align="left">
			<u-form-item label="项目编号" v-if="judgeShow('na002')">
				<u-input v-model="dataForm.na002" disabled></u-input>
			</u-form-item>
			<u-form-item label="项目名称" v-if="judgeShow('na003')">
				<u-input v-model="dataForm.na003" :disabled="judgeWrite('na003')"></u-input>
			</u-form-item>
			<u-form-item label="项目型号" v-if="judgeShow('na004')">
				<u-input v-model="dataForm.na004" :disabled="judgeWrite('na004')"></u-input>
			</u-form-item>
			<u-form-item label="客户名称" v-if="judgeShow('na005')">
				{{na005Name}}
			</u-form-item>
			<u-form-item label="立项部门" v-if="judgeShow('na007')">
				{{na007Name}}
			</u-form-item>
			<u-form-item label="立项申请人" v-if="judgeShow('na008')">
				<jnpf-user-select v-model="dataForm.na008" :disabled="judgeWrite('na008')" />
			</u-form-item>
			<u-form-item label="立项日期" v-if="judgeShow('na009')">
				<u-input v-model="dataForm.na009" :disabled="judgeWrite('na009')"></u-input>
				<lys-date v-if="showna009" type="1" @cancleTime="showna009 = false"
					@submitTime="submitna009"></lys-date>
				<view style="display: flex; justify-content: space-between; width: 100%">
					<view>
						{{ dataForm.na009 }}
					</view>
					<view>
						<u-icon name="arrow-down-fill" color="#ccc" @click="showna009 = true"></u-icon>
					</view>
				</view>
			</u-form-item>
			<u-form-item label="产品分类名称">
				<u-input v-model="dataForm.na021" :disabled="judgeWrite('na021')"></u-input>
			</u-form-item>
			<u-form-item label="优先级" v-if="judgeShow('na011')">
				<u-radio-group v-model="dataForm.na011" :disabled="judgeWrite('na011')">
					<u-radio v-for="(item, index) in na011List" :key="index" :name="item.enCode">
						{{item.fullName}}
					</u-radio>
				</u-radio-group>
			</u-form-item>
			<u-form-item label="要求开发完成天数" v-if="judgeShow('na015')">
				<u-input v-model="dataForm.na015" :disabled="judgeWrite('na015')"></u-input>
			</u-form-item>
			<u-form-item label="开发者方式" v-if="judgeShow('na013')">
				<u-input v-model="dataForm.na013" :disabled="judgeWrite('na013')"></u-input>
			</u-form-item>
			<u-form-item label="认证要求" v-if="judgeShow('na014')">
				<u-input v-model="dataForm.na014" :disabled="judgeWrite('na014')"></u-input>
			</u-form-item>
			<u-form-item label="附件">
				<jnpf-file v-model="dataForm.annex01" :disabled="
			  judgeWrite('annex01')" />
			</u-form-item>
			<u-form-item label="备注" v-if="judgeShow('na020')">
				<u-input v-model="dataForm.na020" :disabled="judgeWrite('na020')"></u-input>
			</u-form-item>

		</u-form>
	</view>
</template>

<script>
	import {
		getPjsna
	} from '@/api/workFlow/flowEngine'
	import {
		getDataInterfaceDataInfo,
		getDictionaryDataSelector,
		getDataInterfaceRes,
		getDepartmentSelector,
		getUserAll,
		Interface,
	} from '@/api/common'

	import comMixin from '../mixin'
	import {
		Info,
		Create,
		Update
	} from '@/api/apply/order'
	import request from '@/utils/request'
	export default {
		name: 'crmOrder',
		// mixins: [comMixin],
		watch: {
			//总需领料量，总未领料量，总领料量
			"dataForm.mocteList": {
				handler(newVal, oldVal) {
					let sumNeed = 0;
					let sumNot = 0;
					let sumTc029 = 0;
					for (let i = 0; i < newVal.length; i++) {
						const e = newVal[i];
						sumNeed += parseFloat(e.ctb_Tb004); //需领用量
						sumNot += parseFloat(e.ctb_Tb004005); //未领用量
						sumTc029 += parseFloat(e.te005); //总领料量
					}
					this.dataForm.sumNeed = sumNeed;
					this.dataForm.sumNot = sumNot;
					this.dataForm.tc029 = sumTc029;
				},
				deep: true,
			},
		},
		data() {
			return {
				na021Name: null,
				na005Name: null,
				na007Name: null,
				showna009: false,
				dataForm: {},
				id: '',
				dataForm: {},
				udf13Name: [],
				tc011Name: [],
				na011List: [],
				na014Options: [],
				tc004Name: '',
				tc006Name: '',
				tc028Name: '',
				tc021Name: ''
			}
		},

		methods: {
			submitna009(time, type, timeInt) {
				let Time = time.split("-");
				this.dataForm.na009 = Time[0] + Time[1] + Time[2];
				this.showna009 = false;
			},
			init(data, id, flowFormContentJson) {
				this.setting = data
				this.getInfo(data)
			},
			getInfo(data) {
				getPjsna(data.id).then(res => {
					if (res.code == 200) {
						this.dataForm = res.data
						this.getna005List()
						this.getna021List(this.dataForm.na021)
						this.getDepartment(this.dataForm.na007)
						this.getSource()
					}
				})
			},
			// 分解树形数据成单个
			flattenTreeDFS(root) {
				const result = [];

				function traverse(node) {
					if (node) {
						result.push(node);
						if (node.children && node.children.length) {
							node.children.forEach(child => traverse(child));
						}
					}
				}
				traverse(root);
				return result;
			},
			// 获取部门
			getDepartment() {
				getDepartmentSelector().then(res => {
					const list = this.flattenTreeDFS(res.data.list[0])
					this.na007Name = list.filter((item) => this.dataForm.na007.includes(item.id)).map(
						(item) =>
						item.fullName)[0];
				})
			},
			getna005List() {
				getDataInterfaceRes('362118781926906437').then(res => {
					this.na005Name = res.data.data.filter((item) => this.dataForm.na005.includes(item.enCode)).map(
						(item) =>
						item.JoinStr)[0];

				})
			},
			getna021List(id) {
				console.log('id', id);
				request({
					url: "/api/SubDev/Pjsmm/1/Actions/List",
					method: "get",
				}).then(res => {
					console.log('res', res.data);
					this.na021Name = this.findItemById(res.data, 'id', id)
					console.log('this.na021Name', this.na021Name);
				})
			},
			findItemById(array, value, id) {
				return array.find(item => item[value] === id);
			},
			judgeShow(id) {
				if (!this.setting.formOperates || !this.setting.formOperates.length)
					return true;
				let arr = this.setting.formOperates.filter((o) => o.id === id) || [];
				if (!arr.length) return true;
				let item = arr[0];
				return item.read;
			},
			judgeWrite(id) {
				if (!this.setting.formOperates || !this.setting.formOperates.length)
					return false;
				let arr = this.setting.formOperates.filter((o) => o.id === id) || [];
				if (!arr.length) return true;
				let item = arr[0];
				return !item.write;
			},
			getUser() {
				getUserAll().then((res) => {
					this.udf13Name = res.data.filter((item) => this.dataForm.udf13.includes(item.id)).map((item) =>
						item.realName);
					this.tc011Name = res.data.filter((item) => this.dataForm.tc028.includes(item.account)).map((
							item) =>
						item.realName);
				});
			},



			getPersonnel(id) {
				let interfaceId = '362114134965230149'
				let data = {
					id,
					interfaceId,
					propsValue: 'MV001',
					relationField: 'MV002',
				}
				getDataInterfaceDataInfo(interfaceId, data).then(res => {
					this.tc028Name = res.data.JoinStr;
				})
			},

			gettc006Name(id) {
				let interfaceId = '381247065990113925'
				let data = {
					id,
					interfaceId,
					propsValue: 'MA001',
					relationField: 'JoinStr',
				}
				getDataInterfaceDataInfo(interfaceId, data).then(res => {
					this.tc006Name = res.data.JoinStr;
				})
			},
			gettc021Name(id) {
				let interfaceId = '362111557984852549'
				let data = {
					id,
					interfaceId,
					propsValue: 'ME001',
					relationField: 'JoinStr',
				}
				getDataInterfaceDataInfo(interfaceId, data).then(res => {
					this.tc021Name = res.data.JoinStr;
				})
			},
			// 获取仓库
			getWarehouse(id, index) {
				let interfaceId = '362114335423601221'
				let data = {
					id,
					interfaceId,
					propsValue: 'MC001',
					relationField: 'MC001',
				}
				getDataInterfaceDataInfo(interfaceId, data).then(res => {
					this.dataForm.mocteList[index].te008 = res.data.MC002
					console.log('this.dataForm.mocteList[index].te008', this.dataForm.mocteList[index].te008);
				})
			},
			getSource() {
				getDictionaryDataSelector("505518936847035461").then(res => {
					this.na011List = res.data.list.map(item => {
						return {
							enCode: item.enCode,
							fullName: item.fullName,
						}
					})
				})
				getDictionaryDataSelector("586344740463677573").then((res) => {
					this.na014Options = res.data.list.map(item => {
						return {
							enCode: item.enCode,
							fullName: item.fullName,
						}
					})
				});
			},

			selfGetInfo() {
				Info(this.setting.id).then(res => {
					this.dataForm = res.data
					if (this.dataForm.fileJson) {
						this.fileList = JSON.parse(this.dataForm.fileJson)
					}
				})
			},
			submit(eventType, config) {
				if (eventType === "audit" || eventType === "reject") {
					this.$emit("eventReceiver", this.dataForm, eventType);
					return;
				}
				this.dataForm.status = eventType === 'submit' ? 0 : 1
				this.dataForm.flowId = this.setting.flowId;
				this.eventType = eventType;
				this.selfHandleRequest()
			},
			selfHandleRequest() {
				uni.navigateBack()
				return
				let paramData = JSON.parse(JSON.stringify(this.dataForm));
				//遍历对象object
				for (let key in paramData.pjsmnList) {
					let e = paramData.pjsmnList[key];
					//判断参数值是否是数组
					if (Array.isArray(e.paramValue) && !!e.paramValue) {
						//将字符串转数组
						e.paramValue = e.paramValue.join(",");
					}
				}
				request({
						url: "/api/SubDev/Pjsna/" + paramData.na001,
						method: "PUT",
						data: paramData,
					})
					.then((res) => {
						uni.showToast({
							title: res.msg,
							complete: () => {
								setTimeout(() => {
									uni.$emit('refresh')
									uni.navigateBack()
								}, 1500)
							}
						})
					})
			},

		}
	}
</script>